using FytSoa.Application.Erp.Param;
using FytSoa.Common.Enum;
using FytSoa.Common.Extensions;
using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-产品服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpProductService : IApplicationService 
{
    private readonly SugarRepository<ErpProduct> _thisRepository;
    public ErpProductService(SugarRepository<ErpProduct> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpProductDto>> GetPagesAsync(ErpProductSearchParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Tag!=ErpProductTagEnum.All,m=>m.ProductTag==param.Tag)
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Name.Contains(param.Key) || m.Number.Contains(param.Key))
            .WhereIF(param.Id!=0,m=>m.TypeId==param.Id)
            .Includes(m=>m.Type)
            .Includes(m=>m.Unit)
            .Includes(m=>m.Employee)
            .Includes(m=>m.Supplier)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ErpProductDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpProductDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m=>m.Employee)
            .Includes(m=>m.Supplier)
            .FirstAsync(m=>m.Id==id);
        return model.Adapt<ErpProductDto>();
    }
    
    /// <summary>
    /// 根据编号查询
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    [HttpGet("{number}")]
    public async Task<ErpProductDto> GetNumberAsync(string number)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m=>m.Employee)
            .Includes(m=>m.Supplier)
            .FirstAsync(m=>m.Number==number);
        return model.Adapt<ErpProductDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task AddAsync(ErpProductDto model)
    {
        if (string.IsNullOrEmpty(model.Number))
        {
            var toDay = await _thisRepository.CountAsync(m=>m.CreateTime.ToString("yyyy-MM")==DateTime.Now.ToString("yyyy-MM"));
            model.Number = "P".ResNumber(toDay, "yyyyMM");
        }
        else
        {
            var isAny = await _thisRepository.IsAnyAsync(m => m.Number == model.Number);
            if (isAny)
            {
                throw new BusinessException("存在相同的产品编码，请更换");
            }
        }
        await _thisRepository.InsertAsync(model.Adapt<ErpProduct>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task ModifyAsync(ErpProductDto model)
    {
        var isAny = await _thisRepository.IsAnyAsync(m => m.Number == model.Number && m.Id != model.Id);
        if (isAny)
        {
            throw new BusinessException("存在相同的产品编码，请更换");
        }
        await _thisRepository.UpdateAsync(model.Adapt<ErpProduct>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
